namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantStyleSheetControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantStyleSheetController>()
                .DerivesFrom<Controller>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<ContentMD5Attribute>()
                .Implements<IRegisterRoutes>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantStyleSheetController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantStyleSheetController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new TenantStyleSheetController() as IRegisterRoutes).Register(routes);

            Assert.IsType<TenantStyleSheetRoute>(routes[0]);
        }

        [Fact]
        public void op_Representation()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<stylesheets>" +
                                "<stylesheet href='/sample/site.css'>" +
                                    "<import>http://static.example.com/site.css</import>" +
                                "</stylesheet>" +
                            "</stylesheets>" +
                        "</tenant>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/site.css").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                StyleSheetResult result = new TenantStyleSheetController().CssRepresentation() as StyleSheetResult;

                Assert.NotNull(result);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Representation_whenNoTenantFound()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>();

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/example/site.css").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                NotFoundResult result = new TenantStyleSheetController().CssRepresentation() as NotFoundResult;

                Assert.NotNull(result);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_Representation_whenNoStylesheets()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/site.css").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                NotFoundResult result = new TenantStyleSheetController().CssRepresentation() as NotFoundResult;

                Assert.NotNull(result);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}